package xhwl.hmo.mall.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import xhwl.hmo.mall.R;
import xhwl.hmo.mall.http.bean.resp.CardStatusResp;
import xhwl.hmo.mall.ui.SuiteProductsDetailActivity;
import xhwl.hmo.mall.utils.GlideRoundTransform;

/**
 * Created by admin on 2017-10-30.
 */

public class SearchAdapter extends BaseAdapter {
    List<CardStatusResp> cardStatuslist;
    Context context;

    public SearchAdapter(Context context, List<CardStatusResp> cardStatuslist) {
        this.context = context;
        this.cardStatuslist = cardStatuslist;
    }

    @Override
    public int getCount() {
        return cardStatuslist.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (holder == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_search, null);
            holder.img_cardbg = (ImageView) convertView.findViewById(R.id.img_sea_cardbg);
            holder.tv_basic_ori_price = (TextView) convertView.findViewById(R.id.tv_sea_ori_price);
            holder.tv_basic_ori_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            holder.tv_basic_salenum = (TextView) convertView.findViewById(R.id.tv_sea_salenum);
            holder.tv_basic_sal_price = (TextView) convertView.findViewById(R.id.tv_sea_sal_price);
            holder.tv_sea_name = (TextView) convertView.findViewById(R.id.tv_sea_name);
            convertView.setTag(holder);
        } else {
            convertView.getTag();
        }
        Glide.with(context).load(cardStatuslist.get(position).GOODS_PIC).transform(new GlideRoundTransform(context)).placeholder(R.drawable.load).into(holder.img_cardbg);
        if(cardStatuslist.get(position).GOODS_MONEY.equals(cardStatuslist.get(position).GOODS_SALE_MONEY)){
            holder.tv_basic_ori_price.setVisibility(View.GONE);
        }
        holder.tv_basic_ori_price.setText("¥ " + cardStatuslist.get(position).GOODS_MONEY);
        holder.tv_basic_sal_price.setText("¥ " + cardStatuslist.get(position).GOODS_SALE_MONEY);
        holder.tv_basic_salenum.setText("已售" + cardStatuslist.get(position).NUM + "件");
        holder.tv_sea_name.setText(cardStatuslist.get(position).GOODS_NAME);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, SuiteProductsDetailActivity.class);
                intent.putExtra("GOOD_ID", cardStatuslist.get(position).GOODS_ID);
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    class ViewHolder {
        ImageView img_cardbg;
        TextView tv_basic_ori_price, tv_basic_salenum, tv_basic_sal_price, tv_sea_name;
    }
}
